<template>
  <div class="userManagement">
    <div class="searchBox">
      <div class="itemBox">
        <div class="itemTitle">医院信息</div>
        <el-input
          class="itemInput"
          v-model="userMsg"
          placeholder="请输入用户姓名/账号/手机号码"
        />
      </div>
      <div class="itemBox">
        <div class="itemTitle">状态</div>
        <el-select v-model="state" placeholder="请选择" class="itemInput">
          <el-option
            v-for="item in stateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="itemBox">
        <div class="itemTitle">创建人</div>
        <el-input
          class="itemInput"
          v-model="founder"
          placeholder="请输入创建人姓名"
        />
      </div>
      <div class="itemBox">
        <div class="itemTitle">创建时间</div>
        <el-date-picker
          v-model="creationTime"
          class="creationTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        <img src="@/assets/images/changeTime.png" alt="" class="timeImg" />
      </div>
      <!--   操作按钮   -->
      <div class="operationBox">
        <div class="resetBox" @click="reset()">重置</div>
        <div class="queryBox">查询</div>
      </div>
    </div>
    <div class="content">
      <div class="addBox" @click="addUser()">新增</div>
      <div class="tableBox">
        <el-table
          :data="tableData"
          style="width: 100%"
          height="600"
          :header-cell-style="{
            background: '#F7F8FA',
            color: '#203549',
          }"
        >
          <el-table-column type="index" label="序号" />
          <el-table-column prop="userName" label="医院名称" />
          <el-table-column prop="userNumber" label="负责人" />
          <el-table-column label="性别">
            <template slot-scope="scope">
              {{ scope.row.gender == 0 ? '女' : '男' }}
            </template>
          </el-table-column>
          <el-table-column prop="phoneNumber" label="负责人电话" />
          <el-table-column prop="role" label="医院账号" />
          <el-table-column label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-color="#0CD13A"
                inactive-color="#D3D3D6"
                @change="changeState(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column prop="founder" label="创建人" />
          <el-table-column prop="creationTime" label="创建时间" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="tableOperation">
                <div @click="handleAccountMsg(scope.row)" class="edit">
                  账号信息
                </div>
                <div @click="queryAccountMsg(scope.row)" class="edit">查看</div>
                <el-dropdown
                  placement="bottom"
                  class="queryMore"
                  trigger="click"
                >
                  <span class="more">…</span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      style="color: #0a73e4; text-align: center"
                      @click.native="handleEdit(scope.row)"
                      >编辑</el-dropdown-item
                    >
                    <el-dropdown-item
                      style="color: #0a73e4; text-align: center"
                      @click.native="modifyPassword(scope.row)"
                      >修改密码</el-dropdown-item
                    >
                    <el-dropdown-item
                      style="color: #ff595a; text-align: center"
                      @click.native="handleDetele(scope.row)"
                      >删除</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <Pagination @getData="getData" :pageData="pageData" />
    </div>

    <!--  新增/编辑/查看医院  -->
    <el-dialog :visible.sync="dialogVisible" width="600px" class="addDialog">
      <div class="dialogContent">
        <div class="header">新增医院</div>
        <div class="formBox">
          <el-form
            :model="ruleForm"
            hide-required-asterisk
            :rules="rules"
            ref="ruleForm"
            label-width="90px"
            class="demo-ruleForm"
          >
            <el-form-item label="医院名称" prop="hospitalName">
              <el-input
                v-model="ruleForm.hospitalName"
                :disabled="dialogType == 2"
                placeholder="请输入内容"
              />
            </el-form-item>
            <el-form-item label="负责人" prop="responsiblePerson">
              <el-input
                v-model="ruleForm.responsiblePerson"
                :disabled="dialogType == 2"
                placeholder="请输入负责人"
              />
            </el-form-item>
            <el-form-item label="负责人电话" prop="phoneNumber">
              <el-input
                v-model="ruleForm.phoneNumber"
                :disabled="dialogType == 2"
                placeholder="请输入内容"
              />
            </el-form-item>
            <el-form-item label="医院账号" prop="HospitalNumber">
              <el-input
                v-model="ruleForm.HospitalNumber"
                :disabled="dialogType == 2"
                placeholder="请输入内容"
              />
            </el-form-item>
            <el-form-item label="医院地址" prop="address">
              <el-cascader
                v-model="ruleForm.address"
                :disabled="dialogType == 2"
                :options="options"
                placeholder="请选择"
                :props="{ expandTrigger: 'hover' }"
                class="input"
                @change="regionChange"
              />
            </el-form-item>
            <el-form-item label="详细地址" prop="sureAddress">
              <el-input
                v-model="ruleForm.sureAddress"
                :disabled="dialogType == 2"
                placeholder="请输入内容"
              />
            </el-form-item>
            <el-form-item label="医院标志" prop="logoUrl">
              <div class="imgBox" v-if="logoUrl">
                <img src="@/assets/images/upload.png" alt="" class="logo" />
              </div>
              <div class="imgBoxUpload" v-if="!logoUrl">
                <img src="@/assets/images/logo.png" alt="" class="logo" />
                <div class="mode" v-if="dialogType != 2">
                  <i class="el-icon-delete iconDel"></i>
                </div>
              </div>
            </el-form-item>
          </el-form>
        </div>
        <div class="hr"></div>
      </div>
      <span slot="footer" class="dialog-footer" v-if="dialogType != 2">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!--  修改密码  -->
    <el-dialog
      :visible.sync="dialogModifyPassword"
      width="600px"
      class="addDialog"
    >
      <div class="dialogContent">
        <div class="header">修改密码</div>
        <div class="formBox">
          <el-form
            :model="modifyPasswordRuleForm"
            hide-required-asterisk
            :rules="rulesRuleForm"
            ref="modifyPasswordRuleForm"
            label-width="90px"
            class="demo-ruleForm"
          >
            <el-form-item label="密码" prop="password">
              <el-input v-model="modifyPasswordRuleForm.password" />
            </el-form-item>
            <el-form-item label="确认密码" prop="surePassword">
              <el-input v-model="modifyPasswordRuleForm.surePassword" />
            </el-form-item>
          </el-form>
        </div>
        <div class="hr"></div>
      </div>
      <span slot="footer" class="dialog-footer" v-if="dialogType != 2">
        <el-button @click="resetModifyForm('modifyPasswordRuleForm')"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="submitModifyForm('modifyPasswordRuleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!--  账号信息  -->
    <el-dialog :visible.sync="dialogAccountMsg" width="600px" class="addDialog">
      <div class="dialogContent">
        <div class="header">账号信息</div>
        <div class="accountMsg">
          <div class="item">
            <div class="itemTitle">医院端链接：</div>
            <div class="itemContent">
              {{ accountMsgForm.link }}
            </div>
          </div>
          <div class="item">
            <div class="itemTitle">登录账号：</div>
            <div>{{ accountMsgForm.userName }}</div>
          </div>
          <div class="item">
            <div class="itemTitle">登录密码：</div>
            <div>{{ accountMsgForm.password }}</div>
          </div>
        </div>
        <div class="hr"></div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="copyAccountMsg"
          >复制账号信息</el-button
        >
        <el-button @click="dialogAccountMsg = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import areaData from '@/utils/js/country-level3-data';
import Pagination from '@/components/Pagination';
export default {
  name: 'HospitalManagement',
  data() {
    return {
      // 新增医院弹窗
      dialogVisible: false,

      // 0--新增医院  1--编辑医院  2--查看详情
      dialogType: 0,

      // 修改密码弹窗
      dialogModifyPassword: false,
      modifyPasswordRuleForm: { password: '', surePassword: '' },
      rulesRuleForm: {
        password: [
          { validator: this.getValidatorPassword(), trigger: 'blur' },
          {
            min: 8,
            max: 20,
            message:
              '请输入8至20位大小写英文字母、特殊符号、数字其中任意三种的组合',
            trigger: 'blur',
          },
        ],
        surePassword: [
          { validator: this.getValidatorSurePassword(), trigger: 'blur' },
          {
            min: 8,
            max: 20,
            message:
              '请输入8至20位大小写英文字母、特殊符号、数字其中任意三种的组合',
            trigger: 'blur',
          },
        ],
      },

      // 账号信息
      dialogAccountMsg: false,

      // 用户信息
      userMsg: '',

      // 状态
      state: '0',
      stateOptions: [
        {
          value: '0',
          label: '全部',
        },
        {
          value: '1',
          label: '开启',
        },
        {
          value: '2',
          label: '禁用',
        },
      ],

      // 创建人
      founder: '',

      //  创建时间
      creationTime: '',

      tableData: [
        {
          userName: '鞠婧祎',
          userNumber: 'jijingyi',
          phoneNumber: '13551793472',
          gender: 0,
          role: '超级管理员',
          state: true,
          founder: '哈哈哈哈',
          creationTime: '2021-12-23',
        },
        {
          userName: '鞠婧祎',
          userNumber: 'jijingyi',
          phoneNumber: '13551793472',
          gender: 0,
          role: '超级管理员',
          state: true,
          founder: '哈哈哈哈',
          creationTime: '2021-12-23',
        },
        {
          userName: '鞠婧祎',
          userNumber: 'jijingyi',
          phoneNumber: '13551793472',
          gender: 0,
          role: '超级管理员',
          state: true,
          founder: '哈哈哈哈',
          creationTime: '2021-12-23',
        },
      ],

      // 传给分页的数据
      pageData: {
        total: 400,
        pageSize: 10,
        currentPage: 1,
      },

      ruleForm: {
        hospitalName: '',
        responsiblePerson: '',
        phoneNumber: '',
        HospitalNumber: '',
        address: '',
        sureAddress: '',
      },
      rules: {
        hospitalName: [
          { required: true, message: '请输入医院名称', trigger: 'blur' },
        ],
        responsiblePerson: [
          { required: true, message: '请输入负责人', trigger: 'blur' },
        ],
        phoneNumber: [
          { validator: this.getValidatorPhoneNumber(), trigger: 'blur' },
        ],
        HospitalNumber: [
          { validator: this.getValidatorUserNumber(), trigger: 'blur' },
        ],
        address: [
          { required: true, message: '请选择医院地址', trigger: 'change' },
        ],
        sureAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
        logoUrl: [{ required: true, message: '请上传图片', trigger: 'blur' }],
      },

      options: areaData,

      logoUrl: '',

      accountMsgForm: {
        link: 'https://www.zcool.com.cn/discover?cate=8&page=1&subCate=15',
        userName: 'zhangyisheng',
        password: 'zhangyisheng999',
      },
    };
  },
  mounted() {
    this.pageData = {
      total: this.tableData.length,
      pageSize: 10,
      currentPage: 1,
    };
  },
  methods: {
    // 验证手机号码
    getValidatorPhoneNumber() {
      return (rule, value, callback) => {
        if (this.ruleForm.phoneNumber === '') {
          callback(new Error('手机号码不能为空!'));
        } else {
          let srt = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
          if (!srt.test(this.ruleForm.phoneNumber)) {
            callback(new Error('手机号码不正确！'));
          }
          callback();
        }
      };
    },

    // 校验医院账号
    getValidatorUserNumber() {
      return (rule, value, callback) => {
        if (this.ruleForm.HospitalNumber === '') {
          callback(new Error('医院账号不能为空!'));
        } else {
          let srt = /^[A-Za-z]+$/;
          if (!srt.test(this.ruleForm.HospitalNumber)) {
            callback(new Error('医院账号只允许输入大小写英文字母!'));
          }
          callback();
        }
      };
    },

    // 校验密码
    getValidatorPassword() {
      return (rule, value, callback) => {
        if (this.modifyPasswordRuleForm.password === '') {
          callback(new Error('密码不能为空!'));
        } else {
          let srt =
            /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{8,15}$/;
          if (!srt.test(this.modifyPasswordRuleForm.password)) {
            callback(
              new Error(
                '请输入8至20位大小写英文字母、特殊符号、数字其中任意三种的组合!'
              )
            );
          }
          callback();
        }
      };
    },

    // 校验确认密码
    getValidatorSurePassword() {
      return (rule, value, callback) => {
        if (this.modifyPasswordRuleForm.surePassword === '') {
          callback(new Error('确认密码不能为空!'));
        } else if (this.modifyPasswordRuleForm.password === '') {
          callback(new Error('请先输入密码！'));
        } else if (
          this.modifyPasswordRuleForm.surePassword !==
          this.modifyPasswordRuleForm.password
        ) {
          callback(new Error('两次密码不一致!'));
        } else {
          callback();
        }
      };
    },

    //选择地址改变
    regionChange(value) {
      // this.editorInfo.province = value[0];
      // this.editorInfo.city = value[1];
      // this.editorInfo.county = value[2];
      console.log(value);
    },

    // 复制账号信息
    copyAccountMsg() {
      let item = this.accountMsgForm;
      let url =
        '医院端链接：' +
        item.link +
        '，' +
        '登录账号：' +
        item.userName +
        '，' +
        '登录密码：' +
        item.password; //拿到想要复制的值
      let copyInput = document.createElement('input'); //创建input元素
      document.body.appendChild(copyInput); //向页面底部追加输入框
      copyInput.setAttribute('value', url); //添加属性，将url赋值给input元素的value属性
      copyInput.select(); //选择input元素
      document.execCommand('Copy'); //执行复制命令
      this.$message.success('账号已复制！'); //弹出提示信息，不同组件可能存在写法不同
      //复制之后再删除元素，否则无法成功赋值
      copyInput.remove(); //删除动态创建的节点
    },

    // 修改密码确定
    submitModifyForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log('修改密码校验');
          this.dialogModifyPassword = false;
          this.resetForm(formName);
        } else {
          return false;
        }
      });
    },

    // 用户新增取消
    resetModifyForm(formName) {
      this.dialogModifyPassword = false;
      this.$refs[formName].resetFields();
    },

    //  重置
    reset() {
      this.userMsg = '';
      this.founder = '';
      this.creationTime = '';
      this.state = '0';
      this.pageData = {
        total: this.tableData.length,
        pageSize: 10,
        currentPage: 1,
      };
    },

    // 新增
    addUser() {
      this.dialogType = 0;
      this.dialogVisible = true;
      this.clearRules('ruleForm');
    },

    // 查看
    queryAccountMsg() {
      this.dialogType = 2;
      this.dialogVisible = true;
    },

    // 删除
    handleDetele(val) {
      this.$confirm('此操作将删除该账号, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          });
        })
        .catch(() => {});
      console.log(val, '删除');
    },

    // 编辑
    handleEdit(command) {
      this.dialogType = 1;
      this.dialogVisible = true;
      this.clearRules('ruleForm');
      console.log(command);
    },

    // 修改密码
    modifyPassword(val) {
      console.log(val, '修改密码');
      this.clearRules('modifyPasswordRuleForm');
      this.dialogModifyPassword = true;
    },

    // 清除校验
    clearRules(ruleName) {
      if (typeof this.$refs[ruleName] !== 'undefined') {
        this.$refs[ruleName].resetFields();
      }
    },

    // 账号信息
    handleAccountMsg(val) {
      console.log(val, '账号信息');
      this.dialogAccountMsg = true;
    },

    // 用户新增确定
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(1111);
        } else {
          return false;
        }
      });
    },

    // 用户新增取消
    resetForm(formName) {
      this.dialogVisible = false;
      this.$refs[formName].resetFields();
    },

    //  分页数据改变
    getData(val) {
      console.log(val, '--分页数据改变--');
    },

    //  删除
    handleDelete() {
      this.$confirm('此操作将删除该账号, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          });
        })
        .catch(() => {});
    },
  },
  components: {
    Pagination,
  },
};
</script>

<style scoped lang="less">
.userManagement {
  .searchBox {
    height: 64px;
    background: #fff;
    padding: 16px;
    box-sizing: border-box;
    display: flex;
    flex-wrap: wrap;
    .itemBox {
      display: flex;
      align-items: center;
      margin-right: 32px;
      position: relative;

      .itemTitle {
        font-size: 14px;
        font-weight: 400;
        color: #111111;
        min-width: 56px;
        text-align: right;
      }
      /deep/ .itemInput {
        margin-left: 8px;

        .el-input__inner {
          width: 240px;
          height: 32px;
        }
        .el-input__icon {
          line-height: 32px;
        }
      }

      /deep/ .creationTime {
        width: 310px;
        height: 32px;
        margin-left: 8px;
        .el-icon-date {
          display: none;
        }
        .el-range-input {
          text-align: left;
        }
        .el-range-separator {
          width: 10%;
          margin-top: -8px;
        }
      }
      .timeImg {
        width: 18px;
        position: absolute;
        right: 8px;
      }
    }
    .operationBox {
      display: flex;
      .resetBox {
        width: 76px;
        height: 32px;
        border-radius: 2px;
        border: 1px solid #dcdfe6;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        font-weight: 400;
        color: #606266;
        cursor: pointer;
        box-sizing: border-box;
      }
      .queryBox {
        width: 76px;
        height: 32px;
        background: #0a73e4;
        border-radius: 2px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        font-size: 14px;
        font-weight: 400;
        color: #ffffff;
        margin-left: 8px;
      }
    }
  }
  .content {
    background: #fff;
    padding: 16px;
    box-sizing: border-box;
    margin-top: 16px;
    height: 800px;
    position: relative;
    .addBox {
      width: 76px;
      height: 32px;
      background: #0a73e4;
      border-radius: 2px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      font-size: 14px;
      font-weight: 400;
      color: #ffffff;
      margin-bottom: 16px;
    }
    .tableBox {
      /deep/ .el-popover {
        width: 88px !important;
        .btnBox {
          width: 88px !important;
        }
      }
      .tableOperation {
        display: flex;
        .edit {
          font-size: 14px;
          font-weight: 400;
          color: #0a73e4;
          cursor: pointer;
          margin-right: 16px;
        }

        /deep/ .el-popper {
          margin-top: 0;
        }
        .queryMore {
          .more {
            font-size: 14px;
            font-weight: 400;
            color: #0a73e4;
            cursor: pointer;
          }
        }
      }
    }
  }
  /deep/ .addDialog {
    .el-dialog__header,
    .el-dialog__body {
      padding: 0;
    }
    .dialogContent {
      .header {
        height: 54px;
        border-bottom: 1px solid #e9e8eb;
        font-size: 16px;
        font-weight: 500;
        color: #101b25;
        line-height: 54px;
        padding-left: 16px;
        box-sizing: border-box;
      }
      .formBox {
        padding: 24px 83px;
        box-sizing: border-box;
        .el-form {
          .el-form-item__label {
            line-height: 32px;
            height: 32px;
          }
          .el-form-item__content {
            line-height: 32px;
            height: 32px;
          }
          .el-input__inner {
            width: 340px;
            height: 32px;
          }
          .el-form-item {
            margin-bottom: 16px;
          }
          .el-input__icon {
            line-height: 32px;
          }
          .imgBox {
            width: 60px;
            height: 60px;
            background: #ffffff;
            border-radius: 2px;
            border: 1px solid #dcdee0;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            .logo {
              width: 20px;
            }
          }
          .imgBoxUpload {
            width: 60px;
            height: 60px;
            background: #ffffff;
            border-radius: 2px;
            border: 1px solid #dcdee0;
            cursor: pointer;
            position: relative;
            .logo {
              width: 60px;
              height: 60px;
              object-fit: cover;
            }
            .mode {
              width: 60px;
              height: 60px;
              background: rgba(17, 17, 17, 0.6);
              border-radius: 2px;
              position: absolute;
              top: 0;
              display: none;
              text-align: center;
              .iconDel {
                color: #fff;
                line-height: 60px;
              }
            }
          }
          .imgBoxUpload:hover > .mode {
            display: block;
          }
        }
      }
      .accountMsg {
        padding: 24px 40px;
        box-sizing: border-box;
        .item {
          display: flex;
          margin-bottom: 8px;

          font-size: 14px;
          color: #111111;
          .itemTitle {
            width: 100px;
            text-align: right;
            margin-right: 8px;
          }
          .itemContent {
            color: #2c89dc;
          }
        }
      }
      .hr {
        height: 2px;
        background: #e9e8eb;
      }
    }
  }
}
</style>
